using System.Xml;
using Microsoft.SqlServer.ReportingServices2005;
using NAnt.Core;
using NAnt.Core.Attributes;

namespace DeployReports.Types
{
	[ElementName("dataSources")]
	public class DataSourceElementCollection : Element
	{
		private DataSourceFileSet fileSet;
		private DataSourceElement[] definition;
		private string pathOnServer;
		private bool overwrite = false;

		[TaskAttribute("pathOnServer", Required = true)]
		public string PathOnServer
		{
			get { return pathOnServer; }
			set { pathOnServer = value; }
		}

		[TaskAttribute("overwrite", Required = false)]
		public bool Overwrite
		{
			get { return overwrite; }
			set { overwrite = value; }
		}

		[BuildElement("fileSet", Required = false)]
		public DataSourceFileSet FileSet
		{
			get { return fileSet; }
			set { fileSet = value; }
		}

		[BuildElementArray("definition", ElementType = typeof (DataSourceElement))]
		public DataSourceElement[] Definition
		{
			get { return definition; }
			set { definition = value; }
		}

//		protected override void InitializeElement(XmlNode elementNode)
//		{
//			base.InitializeElement(elementNode);
//			if (fileSet == null || fileSet.FileNames.Count == 0)
//				if (null == Definition || Definition.Length == 0)
//					throw new BuildException("fileSet and definition cannot both be empty.");
//
//			List<DataSourceElement> dataSources = new List<DataSourceElement>();
//			
//			Array.ForEach(definition, delegate(DataSourceElement element){dataSources.Add(element);});
//			foreach (string fileName in fileSet.FileNames)
//			{
//				DataSourceElement newElement = GetDatasourceFromFile(fileName);
//
//				if (!dataSources.Exists(delegate(DataSourceElement ds) { return ds.DataSourceName == newElement.DataSourceName; }))
//					dataSources.Add(newElement);
//			}
//		}
//

		private DataSourceElement GetDatasourceFromFile(string fileName)
		{
			XmlDocument doc = new XmlDocument();
			doc.Load(fileName);
			XmlNode dataSourceNode = doc.SelectSingleNode("RptDataSource");

			DataSourceElement ds = new DataSourceElement();
			ds.ConnectionString = GetNodeText(dataSourceNode, "ConnectionProperties/ConnectString");

			XmlNode integratedNode = dataSourceNode.SelectSingleNode("ConnectionProperties/IntegratedSecurity");
			if (integratedNode == null || integratedNode.InnerText == "true")
				ds.CredentialRetrieval = CredentialRetrievalEnum.Integrated;
			else
			{
				ds.CredentialRetrieval = CredentialRetrievalEnum.Store;
				AuthenticationElement auth = new AuthenticationElement();
				auth.UserName = fileSet.UserName;
				auth.Password = fileSet.Password;
				auth.Impersonate = true;
				auth.WindowsAccount = fileSet.WindowsAccount;
				ds.AuthenticationElement = auth;
			}
			return ds;
		}

		private static string GetNodeText(XmlNode dataSourceNode, string nodeName)
		{
			return dataSourceNode.SelectSingleNode(nodeName).InnerText;
		}

		private void DeployDataSource(DataSource dataSource, ReportingService2005 rs)
		{
		}
	}
}